/*
 *  PhraseTable.h
 *  OfflineTranslator
 *
 *  Created by PH on 5.9.10.
 *  Copyright 2010 Petr Homola. All rights reserved.
 *
 */

#ifndef NLP_PHRASETABLE_H_
#define NLP_PHRASETABLE_H_

#include "global.h"
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include "Phrase.h"
#include "PhrasePath.h"

namespace nlp {
	
	using namespace std;
#ifdef TR1
	using namespace std::tr1;
#endif
	
	class PhraseTable {
		
	private:
		mutable map<int,vector<Phrase*>*> phrases;
		
	public:
		~PhraseTable() {
			for(map<int,vector<Phrase*>*>::const_iterator it = phrases.begin(); it != phrases.end(); ++it) {
				const vector<Phrase*>* v = it->second;
				if (v != NULL) {
					for (vector<Phrase*>::const_iterator it2 = v->begin(); it2 != v->end(); ++it2) {
						delete *it2;
					}
					delete v;
				} //else cout << "NULL value for node: " << it->first << endl;
			}	
		}
		
		void add(Phrase* phrase) {
			vector<Phrase*>* v = phrases[phrase->getBegin()];
			if (v == NULL) {
				v = new vector<Phrase*>;
				phrases[phrase->getBegin()] = v;
			}
			//v->push_back(phrase);
			v->insert(v->begin(), phrase); // inserting at the beginning because of optimization (ignoring longer paths)
		}
		
		vector<Phrase*>* phrasesBeginningAt(int node) const {
			return phrases[node];
		}
		
		void enumerateSubchains(int length, BLOCKVAR(callback, void, vector<Phrase*>*));
		void enumerateSubchains(int node, int length, BLOCKVAR(callback, void, vector<Phrase*>*)) const;
		unsigned int enumerateShortestPaths(BLOCKVAR(callback, void, PhrasePath*));
		void enumeratePaths(BLOCKVAR(callback, void, PhrasePath*), BLOCKVAR(callback2, void), bool onlyShortest) const;
		void enumeratePaths(int node, map<int, vector<PhrasePath*>*>* cache, BLOCKVAR(callback, void, PhrasePath*), int length, int* minLength) const;
		
	};
	
}

#endif
